/**
 * Copyright (C) 2008 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.wsgi.module;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

import cc.aileron.commons.accessor.ConvertorNotFoundException;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.dao.db.DbConnectionManager;
import cc.aileron.dao.db.DbConnectionManagerSimple;
import cc.aileron.dao.db.logger.NullLogger;
import cc.aileron.dao.db.logger.StdoutLogger;
import cc.aileron.template.sql.impl.SqlLogger;

/**
 * wsgi-db-appliction-module
 * 
 * @author Aileron
 * 
 */
public abstract class WsgiDbApplicationModuleSimple implements
    WsgiDbApplicationModule
{
    /*
     * (�� Javadoc)
     * 
     * @see cc.aileron.wsgi.WsgiDbApplicationModule#dbConnectionManager()
     */
    @Override
    public DbConnectionManager dbConnectionManager()
    {
        return dbConnectionManager;
    }

    /**
     * �ݒ�t�@�C������l�̎擾
     * 
     * @param key
     * @return �ݒ�l
     */
    public String getSetting(final String key)
    {
        return setting.getProperty(key);
    }

    /*
     * (�� Javadoc)
     * 
     * @see cc.aileron.wsgi.WsgiDevelopmentMode#isDevelopment()
     */
    @Override
    public boolean isDevelopment()
    {
        return wsgiDevelopmentMode;
    }

    /*
     * (�� Javadoc)
     * 
     * @see cc.aileron.wsgi.WsgiDbApplicationModule#sqlLogger()
     */
    @Override
    public SqlLogger sqlLogger()
    {
        return sqlLogger;
    }

    /**
     * @return �A�v���P�[�V�����̐ݒ�t�@�C��
     */
    protected abstract Properties getApplictionSettingProperties() throws Exception;

    /**
     * @return DbConnectionManager
     * @throws ClassNotFoundException
     * @throws ResourceNotFoundException
     * @throws ConvertorNotFoundException
     * @throws SQLException
     * @throws IOException
     */
    private DbConnectionManager getDbConnectionManager() throws ClassNotFoundException,
            ResourceNotFoundException,
            ConvertorNotFoundException,
            SQLException,
            IOException
    {
        return new DbConnectionManagerSimple(this.setting);
    }

    /**
     * @return SqlLogger
     */
    private SqlLogger getSqlLogger()
    {
        if (this.wsgiDevelopmentMode)
        {
            return new StdoutLogger();
        }
        return new NullLogger();
    }

    /**
     * constractor
     * 
     * @throws Exception
     */
    public WsgiDbApplicationModuleSimple() throws Exception
    {
        this.setting = getApplictionSettingProperties();
        this.wsgiDevelopmentMode = Boolean.parseBoolean(this.setting
                .getProperty("wsgi.development", "false"));

        this.sqlLogger = getSqlLogger();
        this.dbConnectionManager = getDbConnectionManager();
    }

    /**
     * db-connection-manager
     */
    protected final DbConnectionManager dbConnectionManager;

    /**
     * wsgi-development-module
     */
    protected final boolean wsgiDevelopmentMode;

    /**
     * sql-logger
     */
    protected final SqlLogger sqlLogger;

    /**
     * �ݒ�t�@�C��
     */
    protected final Properties setting;
}
